/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.service.impl;

import me.zhengjie.modules.app.ad.domain.AduserTabsDetail;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.ad.repository.AduserTabsDetailRepository;
import me.zhengjie.modules.app.ad.service.AduserTabsDetailService;
import me.zhengjie.modules.app.ad.service.dto.AduserTabsDetailDto;
import me.zhengjie.modules.app.ad.service.dto.AduserTabsDetailQueryCriteria;
import me.zhengjie.modules.app.ad.service.mapstruct.AduserTabsDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HaojieLi
* @date 2021-03-21
**/
@Service
@RequiredArgsConstructor
public class AduserTabsDetailServiceImpl implements AduserTabsDetailService {

    private final AduserTabsDetailRepository aduserTabsDetailRepository;
    private final AduserTabsDetailMapper aduserTabsDetailMapper;

    @Override
    public Map<String,Object> queryAll(AduserTabsDetailQueryCriteria criteria, Pageable pageable){
        Page<AduserTabsDetail> page = aduserTabsDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aduserTabsDetailMapper::toDto));
    }

    @Override
    public List<AduserTabsDetailDto> queryAll(AduserTabsDetailQueryCriteria criteria){
        return aduserTabsDetailMapper.toDto(aduserTabsDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AduserTabsDetailDto findById(String id) {
        AduserTabsDetail aduserTabsDetail = aduserTabsDetailRepository.findById(id).orElseGet(AduserTabsDetail::new);
        ValidationUtil.isNull(aduserTabsDetail.getId(),"AduserTabsDetail","id",id);
        return aduserTabsDetailMapper.toDto(aduserTabsDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AduserTabsDetailDto create(AduserTabsDetail resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return aduserTabsDetailMapper.toDto(aduserTabsDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AduserTabsDetail resources) {
        AduserTabsDetail aduserTabsDetail = aduserTabsDetailRepository.findById(resources.getId()).orElseGet(AduserTabsDetail::new);
        ValidationUtil.isNull( aduserTabsDetail.getId(),"AduserTabsDetail","id",resources.getId());
        aduserTabsDetail.copy(resources);
        aduserTabsDetailRepository.save(aduserTabsDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            aduserTabsDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AduserTabsDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AduserTabsDetailDto aduserTabsDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("广告商id", aduserTabsDetail.getAdUserId());
            map.put("坑位号", aduserTabsDetail.getTabIndex());
            map.put("左边资源", aduserTabsDetail.getLeftMv());
            map.put("右边资源", aduserTabsDetail.getRightPics());
            map.put("apk配置", aduserTabsDetail.getApkParams());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

	@Override
	public List<Map<String, Object>> findTest() {
		// TODO Auto-generated method stub
		return aduserTabsDetailRepository.findTest();
	}

	@Override
	public List<Object[]> findTest2() {
		// TODO Auto-generated method stub
		return aduserTabsDetailRepository.findTest2();
	}
	@Override
	public List<Object[]> findTest3() {
		// TODO Auto-generated method stub
		return aduserTabsDetailRepository.findTest3();
	}
}